<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学院高基ID" prop="baseCollegeId">
        <el-input
          v-model="queryParams.baseCollegeId"
          placeholder="请输入学院高基ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入实验名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验类别" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入实验类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择实验类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="实验所属学科" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入实验所属学科"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验要求" prop="requirement">
        <el-input
          v-model="queryParams.requirement"
          placeholder="请输入实验要求"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验者类别" prop="experimenterCategory">
        <el-input
          v-model="queryParams.experimenterCategory"
          placeholder="请输入实验者类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人数" prop="personNum">
        <el-input
          v-model="queryParams.personNum"
          placeholder="请输入人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每组人数" prop="personsOfGroup">
        <el-input
          v-model="queryParams.personsOfGroup"
          placeholder="请输入每组人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验学时数" prop="experimentalHours">
        <el-input
          v-model="queryParams.experimentalHours"
          placeholder="请输入实验学时数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验室" prop="labId">
        <el-input
          v-model="queryParams.labId"
          placeholder="请输入实验室"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大纲版本" prop="outlineVersion">
        <el-input
          v-model="queryParams.outlineVersion"
          placeholder="请输入大纲版本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['statistics:statisticsProject:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['statistics:statisticsProject:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['statistics:statisticsProject:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['statistics:statisticsProject:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <el-button
      type="primary"
      icon="el-icon-upload"
      size="mini"
      @click="handleImport"
    >导入</el-button>
    <el-dialog title="基表四数据导入" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :action="upload.url"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :disabled="upload.isUploading"
        :auto-upload="false"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-table v-loading="loading" :data="detailList" height="480px" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="学院高基ID" align="center" prop="baseCollegeId" /> -->
      <el-table-column label="实验名称" align="left" width="240px" prop="name" />
      <el-table-column label="实验类别" align="left"  prop="category" />
      <el-table-column label="实验类型" align="left"  :formatter="typeFormat" />
      <el-table-column label="实验所属学科" align="left"  prop="subject" />
      <el-table-column label="实验要求" align="center" prop="requirement" />
      <el-table-column label="实验者类别" align="center" prop="experimenterCategory" />
      <el-table-column label="人数" align="center" prop="personNum" />
      <el-table-column label="每组人数" align="center" prop="personsOfGroup" />
      <el-table-column label="实验学时数" align="center" prop="experimentalHours" />
      <el-table-column label="实验室" align="center" prop="labId" />
      <el-table-column label="大纲版本" align="center" prop="outlineVersion" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['statistics:statisticsProject:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改教学试验项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :inline=true :model="form" :rules="rules" label-width="120px" style="height: 480px; overflow: auto;">
        <!-- <el-form-item label="学院高基ID" prop="baseCollegeId">
          <el-input v-model="form.baseCollegeId" placeholder="请输入学院高基ID" />
        </el-form-item> -->
        <el-form-item label="实验名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入实验名称" disabled/>
        </el-form-item>
        <el-form-item label="实验类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入实验类别" disabled/>
        </el-form-item>
        <el-form-item label="实验类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择实验类型" disabled>
            <el-option v-for="item in developmentProjectType" :key="item.dictValue" :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实验所属学科" prop="subject">
          <el-input v-model="form.subject" placeholder="请输入实验所属学科" />
        </el-form-item>
        <el-form-item label="实验要求" prop="requirement">
          <el-input v-model="form.requirement" placeholder="请输入实验要求" />
        </el-form-item>
        <el-form-item label="实验者类别" prop="experimenterCategory">
          <el-input v-model="form.experimenterCategory" placeholder="请输入实验者类别" />
        </el-form-item>
        <el-form-item label="人数" prop="personNum">
          <el-input v-model="form.personNum" placeholder="请输入人数" />
        </el-form-item>
        <el-form-item label="每组人数" prop="personsOfGroup">
          <el-input v-model="form.personsOfGroup" placeholder="请输入每组人数" />
        </el-form-item>
        <el-form-item label="实验学时数" prop="experimentalHours">
          <el-input v-model="form.experimentalHours" placeholder="请输入实验学时数" />
        </el-form-item>
        <el-form-item label="实验室" prop="labId">
          <el-input v-model="form.labId" placeholder="请输入实验室" />
        </el-form-item>
        <el-form-item label="大纲版本" prop="outlineVersion">
          <el-input v-model="form.outlineVersion" placeholder="请输入大纲版本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject, exportProject } from "@/api/statistics/statisticsProject";

export default {
  name: "StatisticsProject",
  props: {
    detailList: Array,
    baseCollegeId: Number
  },
  data() {
    return {
      upload: {
        open: false,
        isUploading: false,
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/statistics/project/importData?baseCollegeId=" + this.baseCollegeId
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教学试验项目表格数据
      statisticsProjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        baseCollegeId: null,
        name: null,
        category: null,
        type: null,
        subject: null,
        requirement: null,
        experimenterCategory: null,
        personNum: null,
        personsOfGroup: null,
        experimentalHours: null,
        labId: null,
        outlineVersion: null,
      },
      // 表单参数
      form: {},
      developmentProjectType: [],
      // 表单校验
      rules: {
        baseCollegeId: [
          { required: true, message: "学院高基ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    // development_project_type
    this.getDicts("development_project_type ").then(response => {
      this.developmentProjectType = response.data;
    });
  },
  methods: {
    importTemplate() {
      window.open(process.env.VUE_APP_BASE_API + "/statistics/project/importTemplate")
    },
    handleImport() {
      this.upload.open = true
    },
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
      this.$emit("refresh");
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 项目类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.developmentProjectType, row.type);
    },
    /** 查询教学试验项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.statisticsProjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        baseCollegeId: null,
        name: null,
        category: null,
        type: null,
        subject: null,
        requirement: null,
        experimenterCategory: null,
        personNum: null,
        personsOfGroup: null,
        experimentalHours: null,
        labId: null,
        outlineVersion: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学试验项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教学试验项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$emit("refresh");
            });
          } else {
            addProject(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除教学试验项目编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProject(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教学试验项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProject(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
